package org.darcy.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.darcy.dao.SysResourcesMapper;
import org.darcy.entity.SysResources;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.model.SysResourcesModel;
import org.darcy.service.SysResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 系统资源
 *
 */
@Service
public class SysResourcesServiceImpl extends GenericServiceImpl<SysResources> implements SysResourcesService {

	@Autowired
	private SysResourcesMapper resourceMapper;

	/**
	 * 获取用户的资源列表
	 *
	 * @param map
	 * @return
	 */
	@Override
	public List<SysResourcesModel> listUserResources(Map<String, Object> map) {
		List<SysResourcesModel> sysResources = resourceMapper.listUserResources(map);
		return sysResources;
	}

	/**
	 * 获取ztree使用的资源列表
	 *
	 * @param rid
	 * @return
	 */
	@Override
	public List<Map<String, Object>> queryResourcesListWithSelected(Long rid) {
		List<SysResourcesModel> sysResources = resourceMapper.queryResourcesListWithSelected(rid);
		if (CollectionUtils.isEmpty(sysResources)) {
			return null;
		}
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		for (SysResourcesModel resources : sysResources) {
			map = new HashMap<String, Object>(3);
			map.put("id", resources.getId());
			map.put("pId", resources.getParentId());
			map.put("checked", resources.getChecked());
			map.put("name", resources.getName());
			mapList.add(map);
		}
		return mapList;
	}

	/**
	 * 获取资源的url和permission
	 *
	 * @return
	 */
	@Override
	public List<SysResourcesModel> listUrlAndPermission() {
		List<SysResourcesModel> sysResources = resourceMapper.listUrlAndPermission();
		return sysResources;
	}

	/**
	 * 获取所有可用的菜单资源
	 *
	 * @return
	 */
	@Override
	public List<SysResourcesModel> listAllAvailableMenu() {
		List<SysResourcesModel> sysResources = resourceMapper.listAllAvailableMenu();
		return sysResources;
	}

	/**
	 * 获取用户关联的所有资源
	 *
	 * @param userId
	 * @return
	 */
	public List<SysResources> listByUserId(Long userId) {
		return resourceMapper.listByUserId(userId);
	}

}
